using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MM.Entities;

namespace MM.Business
{
    class clsKHO
    {
        public clsKHO(string sConn)
	    {
		    SqlHelper.ConnectionString = sConn;
	    }
        public List<KHO> DataTableToLstObj(DataTable dt)
        {
            KHO khoDTO;
            List<KHO> lstKhoDTO = new List<KHO>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                khoDTO = new KHO();
                try
                {
                    khoDTO._maKho = dt.Rows[i]["MaKho"].ToString();

                }
                catch (Exception ex)
                {
                }
                try
                {
                    khoDTO._tenKho = dt.Rows[i]["TenKho"].ToString();
                }
                catch (Exception ex)
                {
                }
                try
                {
                    khoDTO._maTau = dt.Rows[i]["MaTau"].ToString();
                }
                catch (Exception ex)
                {
                }
                try
                {
                    khoDTO._maKhoThamChieuSAP = dt.Rows[i]["MaKhoThamChieuSAP"].ToString();
                }
                catch (Exception ex)
                {
                }
                lstKhoDTO.Add(khoDTO);
            }
            return lstKhoDTO;
        }
        public KHO DataTableToObj(DataTable dt)
        {
            KHO khoDTO = new KHO();
            try
            {
                khoDTO._maKho = dt.Rows[0]["MaKho"].ToString();

            }
            catch (Exception ex)
            {
            }
            try
            {
                khoDTO._tenKho = dt.Rows[0]["TenKho"].ToString();
            }
            catch (Exception ex)
            {
            }
            try
            {
                khoDTO._maTau = dt.Rows[0]["MaTau"].ToString();
            }
            catch (Exception ex)
            {
            }
            try
            {
                khoDTO._maKhoThamChieuSAP = dt.Rows[0]["MaKhoThamChieuSAP"].ToString();
            }
            catch (Exception ex)
            {
            }
            return khoDTO;
        }
        public List<KHO> SelectKHOsAll()
        {
            DataTable dt = new DataTable();
            List<KHO> lstKhoDTO = new List<KHO>();
            try
            {
                SqlHelper.OpenConnection();
                string spName = "usp_SelectKHOsAll";
                dt = SqlHelper.ExecuteToDataTable(spName, CommandType.StoredProcedure);
                lstKhoDTO = DataTableToLstObj(dt);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return lstKhoDTO;
        }
        public List<KHO> LoadKHOByTAU(string id)
        {
            DataTable dt = new DataTable();
            List<KHO> lstKhoDTO = new List<KHO>();
            try
            {
                SqlHelper.OpenConnection();
                string spName = "usp_SelectKHOByTAU";
                SqlParameter[] par = new SqlParameter[1];
                par[0] = new SqlParameter("@MaTau", id);
                dt = SqlHelper.ExecuteToDataTable(spName, par);
                lstKhoDTO = DataTableToLstObj(dt);
            }

            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return lstKhoDTO;
        }

        #region Yen
        public List<KHO> usp_SelectKHO_ByMaVTPT_TON_KHO(string mavtpt)
        {
            DataTable dt = new DataTable();
            List<KHO> lstKhoDTO = new List<KHO>();
            try
            {
                SqlHelper.OpenConnection();
                string spName = "usp_SelectKHO_ByMaVTPT_TON_KHO";
                SqlParameter[] par = new SqlParameter[1];
                par[0] = new SqlParameter("@MaVTPT", mavtpt);
                dt = SqlHelper.ExecuteToDataTable(spName, par);
                lstKhoDTO = DataTableToLstObj(dt);
            }

            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return lstKhoDTO;
        }

        public KHO usp_SelectKHO_ByMaKho(string makho)
        {
            DataTable dt = new DataTable();
            List<KHO> lstKhoDTO = new List<KHO>();
            try
            {
                SqlHelper.OpenConnection();
                string spName = "select * from kho where makho='"+makho+"'";
               
                dt = SqlHelper.ExecuteToDataTable(spName, CommandType.Text);
                lstKhoDTO = DataTableToLstObj(dt);
               
            }

            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            if (lstKhoDTO.Count == 0)
                return null;
            return lstKhoDTO[0];
        }
        #endregion Yen
    }
}
